package com.epam.springtest.service;

import com.epam.springtest.dao.UserDao;
import com.epam.springtest.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Mikalai Kisel
 */
@Service("userService")
@Repository
@Transactional
public class UserServiceImpl implements UserService {

    private final String irregularEmail = "1";
    private final String irregularPassword = "2";
    private final String success = "3";

    @Autowired
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public String checkUser(String email, String password) {
        User user = userDao.findUserByEmail(email);
        if (user == null)
            return irregularEmail;
        else {
            if (user.getPassword().equals(password))
                return success;
            else
                return irregularPassword;
        }

    }

    @Override
    public void insertUser(User user) {
        userDao.insert(user);
    }
}
